<?php
include dirname(dirname(__FILE__))."/include/xoops.php";
$module_name = basename( dirname(dirname( __FILE__ ))) ;

	$error=0;
	if (!file_exists(XOOPS_ROOT_PATH."/modules/".$module_name."/templates/".$module_name."_index.html")) {
	  echo "<font color='red'>Template ".$xoops_um_ROOT."/modules/".$module_name."/templates/".$module_name."_index.html not exists!</font><br />";
		$error++;
	} 
	if (!file_exists(XOOPS_ROOT_PATH."/modules/".$module_name."/templates/blocks/".$module_name."_freiblock.html")) {
	  echo "<font color='red'>Template ".$xoops_um_ROOT."/modules/".$module_name."/templates/blocks/".$module_name."_freiblock.html not exists!</font><br />";
		$error++;
	} 
	if (!file_exists(XOOPS_ROOT_PATH."/modules/".$module_name."/templates/".$module_name."_index.html")) {
	  echo "<font color='red'>Template ".$xoops_um_ROOT."/modules/".$module_name."/templates/blocks/".$module_name."_nav_block.html not exists!</font><br />";
		$error++;
	} 
	if ($error>0) {
	  if (xoops_check_version() < 0) 
		  redirect_header(XOOPS_URL."/modules/legacy/admin/index.php?action=ModuleList", 3, sprintf(_MD_AM_FAILUPD,$module_name));
		else 
	    redirect_header(XOOPS_URL."/modules/system/admin.php?fct=modulesadmin", 3, sprintf(_MD_AM_FAILUPD,$module_name));
	  exit();
	}

  global $xoopsDB;
	if (!InfoTableExists($xoopsDB->prefix($module_name.'_cat'))) {
	  $sql= "CREATE TABLE ".$xoopsDB->prefix($module_name.'_cat')." (
             catid int(8) NOT NULL auto_increment,
             visible tinyint(1) NOT NULL default '0',
             title varchar(255) NOT NULL default '',
						 PRIMARY KEY  (catid)
           )TYPE=MyISAM;";
	  $result = $xoopsDB->queryF($sql);
		if (!$result) {
		  echo "<font color='red'>Can't create Table '".$xoopsDB->prefix($module_name.'_cat')."'</font><br />";
		} else {
		  echo "<font color='green'>Table '".$xoopsDB->prefix($module_name.'_cat')."' created</font><br />";
		  $sql="INSERT INTO ".$xoopsDB->prefix($module_name.'_cat')." (catid,title) VALUES (1,'Default')";
			$result = $xoopsDB->queryF($sql);
		  if (!$result) echo "<font color='red'>Can't insert Data in  Table '".$xoopsDB->prefix($module_name.'_cat')."'</font><br />";
		}
	} else {
	  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_cat ADD catid int(8) NOT NULL auto_increment";
	  $result = $xoopsDB->query($sql);
		if (!$result) {
		  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_cat CHANGE catid catid int(8) NOT NULL auto_increment";
	    $result = $xoopsDB->query($sql);
		}
	  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_cat ADD visible tinyint(1) NOT NULL default '0'";
	  $result = $xoopsDB->query($sql);
		if (!$result) {
		  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_cat CHANGE visible visible tinyint(1) NOT NULL default '0'";
	    $result = $xoopsDB->query($sql);
		}
	  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_cat ADD title varchar(255) NOT NULL default ''";
	  $result = $xoopsDB->query($sql);
		if (!$result) {
		  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_cat CHANGE title title varchar(255) NOT NULL default ''";
	    $result = $xoopsDB->query($sql);
		}
		echo "<font color='green'>Updatetd Table '".$xoopsDB->prefix($module_name)."_cat'</font><br />";
	}
	
	if (!InfoTableExists($xoopsDB->prefix($module_name))) {
	  $sql= "CREATE TABLE ".$xoopsDB->prefix($module_name)." (
             storyid int(8) NOT NULL auto_increment,
						 st int(2) NOT NULL default '0',
             parent_id int(8) NOT NULL default '0',
             blockid int(8) unsigned NOT NULL default '0',
						 frontpage tinyint(1) NOT NULL default '0',
             title varchar(255) NOT NULL default '',
             text text default NULL,
             visible tinyint(1) NOT NULL default '0',
             homepage int(8) NOT NULL default '0',
             nohtml tinyint(1) NOT NULL default '0',
             nosmiley tinyint(1) NOT NULL default '0',
             nobreaks tinyint(1) NOT NULL default '0',
             nocomments tinyint(1) NOT NULL default '0',
             link tinyint(1) NOT NULL default '0',
             address varchar(255) default NULL,
             submenu tinyint(1) NOT NULL default '0',
             visible_group text default NULL,
             edited_time int(15) NOT NULL default '0',
             edited_user int(15) NOT NULL default '0',
						 click tinyint(1) NOT NULL default '0',
						 self tinyint(1) NOT NULL default '0',
						 frame text default NULL,
						 tooltip text default NULL,
						 title_sicht tinyint(1) NOT NULL default '1',
						 footer_sicht tinyint(1) NOT NULL default '1',
						 owner int(15) NOT NULL default '0',
						 bl_left int(2) NOT NULL default '1',
						 bl_right int(2) NOT NULL default '1',
             PRIMARY KEY  (storyid),
             KEY title (title(40))
           ) TYPE=MyISAM;";
		$result = $xoopsDB->queryF($sql);
		if (!$result) {
		  echo "<font color='red'>Can't create Table '".$xoopsDB->prefix($module_name)."'</font><br />";
		} else {
		  echo "<font color='green'>Table '".$xoopsDB->prefix($module_name)."' created</font><br />";
		}
	} else {
	  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD frontpage tinyint(1) NOT NULL default '0'";
		$result = $xoopsDB->queryF($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD click tinyint(1) NOT NULL default '0'";
		$result = $xoopsDB->queryF($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD self tinyint(1) NOT NULL default '0'";
		$result = $xoopsDB->queryF($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD frame text default ''";
		$result = $xoopsDB->queryF($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD title_sicht tinyint(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD edited_user int(12) NOT NULL default '0'";
	  $result = $xoopsDB->query($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD edited_time int(12) NOT NULL default '0'";
	  $result = $xoopsDB->query($sql);
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD footer_sicht tinyint(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);	
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD tooltip text default ''";
	  $result = $xoopsDB->query($sql);	
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD owner int(12) NOT NULL default '0'";
	  $result = $xoopsDB->query($sql);	
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD st int(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);	
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD bl_left int(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);	
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)." ADD bl_right int(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);	
		echo "<font color='green'>Updatetd Table '".$xoopsDB->prefix($module_name)."'</font><br />";
	}	
	
	if (!InfoTableExists($xoopsDB->prefix($module_name)."_bak")) {
	  $sql= "CREATE TABLE ".$xoopsDB->prefix($module_name)."_bak (
		         bakid int(8) NOT NULL auto_increment,
						 storyid int(8) NOT NULL default '0',
             st int(2) NOT NULL default '1',
						 sub_time int(15) NOT NULL default '0',
             parent_id int(8) NOT NULL default '0',
             blockid int(8) unsigned NOT NULL default '0',
						 frontpage tinyint(1) NOT NULL default '0',
             title varchar(255) NOT NULL default '',
             text text default NULL,
             visible tinyint(1) NOT NULL default '0',
             homepage int(8) NOT NULL default '0',
             nohtml tinyint(1) NOT NULL default '0',
             nosmiley tinyint(1) NOT NULL default '0',
             nobreaks tinyint(1) NOT NULL default '0',
             nocomments tinyint(1) NOT NULL default '0',
             link tinyint(1) NOT NULL default '0',
             address varchar(255) default NULL,
             submenu tinyint(1) NOT NULL default '0',
             visible_group text default NULL,
             edited_time int(15) NOT NULL default '0',
             edited_user int(15) NOT NULL default '0',
						 click tinyint(1) NOT NULL default '0',
						 self tinyint(1) NOT NULL default '0',
						 frame text default NULL,
						 tooltip text default NULL,
						 title_sicht tinyint(1) NOT NULL default '1',
						 footer_sicht tinyint(1) NOT NULL default '1',
						 owner int(15) NOT NULL default '0',
						 bl_left int(2) NOT NULL default '1',
						 bl_right int(2) NOT NULL default '1',
             PRIMARY KEY  (bakid),
             KEY title (title(40))
           ) TYPE=MyISAM;";
		$result = $xoopsDB->queryF($sql);
		if (!$result) {
		  echo "<font color='red'>Can't create Table '".$xoopsDB->prefix($module_name)."_bak'</font><br />";
		} else {
		  echo "<font color='green'>Table '".$xoopsDB->prefix($module_name)."_bak' created</font><br />";
		}
  } else {
	  $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_bak ADD owner int(15) NOT NULL default '0'";
	  $result = $xoopsDB->query($sql);	
		$sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_bak ADD bl_left int(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);
    $sql="ALTER TABLE ".$xoopsDB->prefix($module_name)."_bak ADD bl_right int(2) NOT NULL default '1'";
	  $result = $xoopsDB->query($sql);
		echo "<font color='green'>Updatetd Table '".$xoopsDB->prefix($module_name)."_bak'</font><br />";
	}	
?>